package com.liuche.service;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @author：liuche
 * @Date：2024年08月25日 09:45
 * @Description：
 */
@Service
@Slf4j
public class ArthasService {
    /**
     * 创建线程池
     */
    private static final ExecutorService threadPool = Executors.newCachedThreadPool();

    /**
     * 两把锁
     */
    private final Object lockA = new Object();
    private final Object lockB = new Object();

    private void cpuHigh() {

        // 添加到线程
        threadPool.submit(() -> {
            while (true) {
                log.info("cpu working");
            }
        });
    }

    /**
     * 测试线程死锁
     */
    public void testDeadLock() {
        new Thread(() -> {
            // 先拿 lockA
            synchronized (lockA) {
                log.info("deadLock-A success get lock ==>lockA");
                try {
                    TimeUnit.SECONDS.sleep(5);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                // 后拿 lockB
                synchronized (lockB) {
                    log.info("deadLock-A success get lock ==>lockB");
                }
            }
        }, "deadLock-A").start();

        new Thread(() -> {
            // 先拿 lockB
            synchronized (lockB) {
                log.info("deadLock-B success get lock ==>lockB");
                try {
                    TimeUnit.SECONDS.sleep(5);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                // 再拿 lockA
                synchronized (lockA) {
                    log.info("deadLock-B success get lock ==>lockA");
                }
            }
        }, "deadLock-B").start();
    }

    public JSONObject queryUser(Integer uid) throws Exception {
        check(uid);
        service(uid);
        redis(uid);
        return mysql(uid);
    }


    /**
     * 模拟CPU沾满
     */
    public void cpuFull() {
        cpuHigh();
        loopThread();
    }

    private void loopThread() {
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                while (true) {
                    log.info("死循环线程工作中");
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }, "thread" + i).start();
        }
    }

    public void service(Integer uid) throws Exception {
        log.info("调用其他service。。。。。");
        TimeUnit.SECONDS.sleep(RandomUtil.randomLong(1, 2));
    }

    public void redis(Integer uid) throws Exception {
        log.info("查看redis缓存数据。。。。。");
        TimeUnit.MILLISECONDS.sleep(RandomUtil.randomInt(100, 200));
    }

    public JSONObject mysql(Integer uid) throws Exception {
        log.info("查询MySQL数据......");
        TimeUnit.SECONDS.sleep(RandomUtil.randomInt(3, 4));
        JSONObject jsonObject = new JSONObject();
        jsonObject.putOnce("name", "xiaoming");
        jsonObject.putOnce("age", 18);
        return jsonObject;
    }

    public boolean check(Integer uid) throws Exception {
        if (uid == null || uid < 0) {
            log.error("非法用户id，uid:{}", uid);
            throw new Exception("非法用户id");
        }
        return true;
    }
}
